//引入VueRouter
import VueRouter from 'vue-router'
import vue from 'vue'
import { Message } from 'element-ui';

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
vue.use(VueRouter, Message)

import Error from '@/pages/Error'
import Home from "../pages/Home.vue"
import Login from '../pages/Login.vue'
import Register from "../pages/Register.vue"
import Sign from "../pages/sgin.vue"
import PersonalInfo from "../components/personalInfo/personalInfo.vue"
import Uploadimg from "../components/upload.vue"
import Portal from '@/components/Store/Portal'
import Detail from '@/components/Store/Detail/Detail'
import ShopCart from '@/components/Store/ShopCart/ShopCart'
import Confirmcart from '@/components/Store/Order/Confirmcart'
import Order from '@/components/Store/Order/Order'
import Forget from '@/pages/Forget'
import Pay from '../components/Store/Order/Pay.vue'
//创建router实例对象，去管理一组一组的路由规则
const router = new VueRouter({
    mode: 'history',
    routes: [
        {
            path: '/login',
            name: "Login",
            component: Login
        },
        {
            path: '/register',
            name: "Register",
            component: Register
        },
        {
            path: '/sign',
            name: "Sign",
            component: Sign
        },
        {
            path: '/home',
            name: "Home",
            component: Home,
            redirect: '/portal',
            children: [
                {
                    path: '/personalInfo',
                    name: "PersonalInfo",
                    component: PersonalInfo,
                },
                {
                    path: '/upload',
                    name: 'Upload',
                    component: Uploadimg
                },
                {
                    path: '/portal',
                    name: 'Portal',
                    component: Portal,
                },
                {
                    path: '/detail',
                    name: 'Detial',
                    component: Detail
                },
                {
                    path: '/shopCart',
                    name: 'ShopCart',
                    component: ShopCart
                },
                {
                    path: '/confirmcart',
                    name: 'Confirmcart',
                    component: Confirmcart,
                    beforeEnter: (to, from, next) => {
                        if (localStorage.getItem('shopcart') == null) {
                            next('/error')
                        } else {
                            next()
                        }
                    }
                }, {
                    path: '/order',
                    name: 'Order',
                    component: Order
                }, {
                    path: '/pay',
                    name: "Pay",
                    component: Pay,
                    beforeEnter: (to, from, next) => {
                        if (localStorage.getItem('ordercart') == null) {
                            next('/error')
                        } else {
                            next()
                        }
                    }
                }
            ]
        },
        {

            path: '/forget',
            name: "Forget",
            component: Forget
        },
        {
            path: '/error',
            name: "Error",
            component: Error
        }
    ],

})

router.beforeEach((to, from, next) => {
    if (to.path === '/login' || to.path === '/register' || to.path === '/sign' || to.path === '/forget' || to.path === '/error') {
        next();
    } else {
        let token = localStorage.getItem('token');
        if (token === null || token === '') {
            Message({
                message: '请先登录，在进行操作哦！',
                type: 'warning',
                showClose: true,
            })
            setTimeout(() => {
                next('/login');
            }, 1000)
        } else {
            next();
        }
    }
});

export default router